package com.teamblueplus.comicmaker;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * DrawingLayer represents on layer of image or bubble on the canvas each one
 * has location, bitmap, width, height type: 1 = image 2 = bubble
 */
public class DrawingLayer {

	int xLoc, yLoc;
	Bitmap bitmap;
	Bitmap savedBitmap;
	int width, height;
	int type;

	int rectX, rectY;

	// constructor for image
	public DrawingLayer(int type, String path) {
		xLoc = 100;
		yLoc = 100;
		// bitmap = decodeSampledBitmapFromResource(path, 350, 350);

		this.type = type;
		bitmap = BitmapFactory.decodeFile(path);

		// load only subsample of image
		//bitmap = decodeSampledBitmapFromFile(path, 240, 135);
		width = bitmap.getWidth();
		height = bitmap.getHeight();

		// resize image to smaller
		if (width > 450 || height > 450) {
			int scaledX, scaledY;
			float scaled = (float) width / (float) height;
			if (width == height) {
				scaledX = 450;
				scaledY = 450;
			} else if (width > height) {
				scaledX = 450;
				scaledY = (int) (450.0 / scaled);
			} else {
				scaledY = 450;
				scaledX = (int) (450.0 * scaled);
			}

			width = scaledX;
			height = scaledY;

			// resize
			bitmap = Bitmap.createScaledBitmap(bitmap, scaledX, scaledY, true);

		}

		savedBitmap = Bitmap.createBitmap(bitmap);
	}

	// constructor for image
	public DrawingLayer(int type, Bitmap imageBitmap) {

		bitmap = imageBitmap;
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		xLoc = 120;
		yLoc = 200;
		this.type = type;
		
		if (type == 2) {

			// resize image to smaller
			if (width > 480 || height > 480) {
				int scaledX, scaledY;
				float scaled = (float) width / (float) height;
				if (width == height) {
					scaledX = 480;
					scaledY = 480;
				} else if (width > height) {
					scaledX = 480;
					scaledY = (int) (480.0 / scaled);
				} else {
					scaledY = 480;
					scaledX = (int) (480.0 * scaled);
				}

				width = scaledX;
				height = scaledY;

				// resize
				bitmap = Bitmap.createScaledBitmap(bitmap, scaledX, scaledY,
						false);

			}
		} 
		savedBitmap = Bitmap.createBitmap(bitmap);
	}


	/**
	 * resize the image from the pinch zoom amount
	 * 
	 * @param initial
	 * @param current
	 */
	public void resize(float initial, float current) {
		float curScale = current / initial;
		if (curScale < 0.1) {
			curScale = 0.1f;
		}

		int newHeight = (int) (height * curScale);
		int newWidth = (int) (width * curScale);

		if (newHeight < 70 || newWidth < 70){
			return;
		}
		
		bitmap = Bitmap.createScaledBitmap(savedBitmap, newWidth, newHeight,
				false);

	}
	
	public void setNewSize(){
		width = bitmap.getWidth();
		height = bitmap.getHeight();
	}
	

	public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
			int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}

		// Calculate inSampleSize
		options.inSampleSize = inSampleSize;

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;

		Bitmap temp = BitmapFactory.decodeFile(path, options);

		return temp;
	}

	// resample
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 2;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}

		return inSampleSize;
	}

	public static Bitmap decodeSampledBitmapFromResource(String resId,
			int reqWidth, int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(resId, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(resId, options);
	}

	// ---- getters and setters -----
	public int getxLoc() {
		return xLoc;
	}

	public void setxLoc(int xLoc) {
		this.xLoc = xLoc;
	}

	public int getyLoc() {
		return yLoc;
	}

	public void setyLoc(int yLoc) {
		this.yLoc = yLoc;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}
